package com.mjk.manager.usercenter.scmenu;


import cn.hutool.core.util.StrUtil;
import com.mjk.common.base.constns.BeeCommonConstns;
import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.base.work.BaseWork;
import com.mjk.common.base.work.annotation.BeeApi;
import com.mjk.common.base.work.annotation.BeeWork;
import com.mjk.common.core.jdbc.service.idgenerator.IdGenerateType;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeComplexQueryWrapper;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.tools.logger.BizLogger;

import com.mjk.common.util.TreeUtils;
import com.mjk.manager.amis.core.dto.AmisCrudDTO;
import com.mjk.manager.amis.core.dto.RoleMenuDto;
import com.mjk.manager.amis.core.dto.SessionDetails;
import com.mjk.manager.amis.core.model.SysMenu;
import com.mjk.manager.amis.core.model.SysRoleMenu;
import com.mjk.manager.usercenter.scmenu.dto.ScMenuDto;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@BeeWork(work = "scmenu")
@SuppressWarnings("ALL")
public class ScMenuWork extends BaseWork {
	@Autowired
	private BeeDBService beeDBService;
	/**
	 * 获取菜单信息
     */
	@BeeApi(name = "获取系统菜单")
	public BeeResponse getScMenu(ScMenuDto dto) {
		BeeQueryWrapper wrapper = new BeeQueryWrapper(new SysMenu());
		List<SysMenu> menuList = beeDBService.findManyByWrapper(wrapper);
		List<SysMenu> menuTree = TreeUtils.list2Tree(menuList, "0", "id", "PID", "children", false);
		final AmisCrudDTO<SysMenu> data = new AmisCrudDTO<>();
		data.setItems(menuTree);
		return BeeError.BEE_SUC.tranceError(data);
	}
	/**
	 * 保存菜单
     */
	public BeeResponse saveScMenu(SysMenu model) {
		model.setUPDATED_BY(model.getCreater());
		model.setUPDATED_AT(LocalDateTime.now());
		int ret = 0;
		if (StrUtil.isNotBlank(model.getId())) {
			// update
			BeeQueryWrapper wrapper = new BeeQueryWrapper(model);
			wrapper.eq(SysMenu::getId, model.getId());
			ret = beeDBService.update(wrapper);
		} else {
			model.setDELETED(BeeCommonConstns.DEFAULT_UNDEL);
			model.setId(IdGenerateType.AUTO_SNOWFLAKE.getGenerator().getId(model));
			model.setCREATED_BY(model.getCreater());
			model.setCREATED_AT(LocalDateTime.now());
			ret = beeDBService.save(model);
		}
		return ret > 0 ? BeeError.BEE_SUC.tranceError(model.getId()) : BeeError.BEE_FAILD.tranceError();
	}


	
	public BeeResponse batchUp(SysMenu model) {
		needParams(model, SysMenu::getId);
		BeeQueryWrapper wrapper = new BeeQueryWrapper(SysMenu.class);
		wrapper.in(SysMenu::getId, Arrays.asList(model.getId().split(",")));
		int ret = beeDBService.delete(wrapper);
		return ret > 0 ? BeeError.BEE_SUC.tranceError() : BeeError.BEE_FAILD.tranceError();
	}

	/**
	 * 角色对应的菜单
	 *
	 * @param roles
	 * @return
	 */
	public BeeResponse getMenuByRole(ScMenuDto dto) {
		BeeComplexQueryWrapper wrapper1 = new BeeComplexQueryWrapper(SysMenu.class, "a");
		wrapper1.innerJoin(SysRoleMenu.class, "b").on(SysMenu::getId, SysRoleMenu::getMENU_ID);
		wrapper1.eq(SysRoleMenu::getROLE_ID, "b", dto.getRoleId());
		List<SysMenu> menus = beeDBService.findManyByComplexWrapper(wrapper1);
		return BeeError.BEE_SUC.tranceError(menus);
	}

	public BeeResponse saveRoleMenu(ScMenuDto dto) {
		if (!CollectionUtils.isEmpty(dto.getMenuIds())) {
			//先删除
			BeeQueryWrapper wrapper = new BeeQueryWrapper(SysRoleMenu.class);
			wrapper.eq(SysRoleMenu::getROLE_ID, dto.getRoleId());
			beeDBService.delete(wrapper);
			List<SysRoleMenu> roleMenus = new ArrayList<>();
			for (String menuId : dto.getMenuIds()) {
				SysRoleMenu roleMenu = new SysRoleMenu();
				roleMenu.setROLE_ID(dto.getRoleId()).setMENU_ID(menuId).setId(IdGenerateType.AUTO_SNOWFLAKE.getGenerator().getId(roleMenu))
						.setDELETED(false).setCREATED_BY(dto.getCreater()).setCREATED_AT(LocalDateTime.now())
						.setUPDATED_BY(dto.getCreater()).setUPDATED_AT(LocalDateTime.now());
				roleMenus.add(roleMenu);
			}
			beeDBService.save(roleMenus);
		}
		return BeeError.BEE_SUC.tranceError();
	}
}
